--require "MainScene"
AddPowerCashScene={}
AddPowerCashSceneFunc={}
AddPowerCashSceneFunc.__index = AddPowerCashSceneFunc
setmetatable(AddPowerCashScene, AddPowerCashSceneFunc)

AddPowerCashSceneFunc.createAddPowerDialog = function()
  local l = {}
  setmetatable(l, AddPowerCashSceneFunc)
  l.node = CCBReader:load("PaoZaoDialog.ccbi")
  setObjectExtTable(l.node:getChildByTag(1020), l)
  setObjectExtTable(l.node:getChildByTag(1111), l)
  NetworkRequest.initExPower("onAddPowerDialogComplete", l)
  LoadScene:showLoadingScene()
  --setTouchEnabledAll(l.node, false)
  return l
end
AddPowerCashSceneFunc.onAddPowerDialogComplete = function(self, data)
  print("AddPowerCashSceneFunc.onAddPowerDialogComplete", data)
  LoadScene:removeLoadingScene()
  local res = cjson.decode(data)
  if res.ret==0 then
    setTouchEnabledAll(self.node, true)
    self.coin = res.data.nextPayCoin
    self.power = res.data.nextAddPower
    TypeCast(self.node:getChildByTag(1011), "CCLabelTTF"):setString(string.format("是否花费%d军饷，增加%d点体力", self.coin, self.power))
  else
    showMessage(res.msg, ccc3(255,255,0))
    self.node:getParent():removeChild(self.node, true)
  end
end
AddPowerCashSceneFunc.onAddPowerButton = function(self, node, event)
  --TypeCast(self.node:getChildByTag(1020), "CCButton"):setEnabled(false)
  LoadScene:showLoadingScene()
  NetworkRequest.exchangePower(self.coin, self.power, "onAddPowerComplete", self)
end
AddPowerCashSceneFunc.onAddPowerComplete = function(self, data)
  print("AddPowerCashSceneFunc.onAddPowerComplete", data)
  LoadScene:removeLoadingScene()
  --TypeCast(self.node:getChildByTag(1020), "CCButton"):setEnabled(true)
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    self.coin = res.data.nextPayCoin
    self.power = res.data.nextAddPower
    TypeCast(self.node:getChildByTag(1011), "CCLabelTTF"):setString(string.format("是否花费%d军饷，增加%d点体力", self.coin, self.power))
  else
    showMessage(res.msg, ccc3(255,255,0))
  end 
end

AddPowerCashSceneFunc.onCloseButton = function(self, node, event)
  MainScene.scene:removeChild(node:getParent(), true)
  setTouchEnabledAll(MainScene.node, true)
  MainScene.setState()
end
------------------
AddPowerCashSceneFunc.createAddCashDialog = function()
  local l = {}
  setmetatable(l, AddPowerCashSceneFunc)
  l.node = CCBReader:load("TaoJinDialog.ccbi")
  setObjectExtTable(l.node:getChildByTag(1020), l)
  setObjectExtTable(l.node:getChildByTag(1111), l)
  NetworkRequest.initExchange("onAddCashDialogComplete", l)
  LoadScene:showLoadingScene()
  --setTouchEnabledAll(l.node, false)
  return l
end
AddPowerCashSceneFunc.onAddCashDialogComplete = function(self, data)
  print("AddPowerCashSceneFunc.onAddPowerDialogComplete", data)
  LoadScene:removeLoadingScene()
  local res = cjson.decode(data)
  if res.ret==0 then
    --setTouchEnabledAll(self.node, true)
    self.coin = res.data.nextPayCoin
    self.cash = res.data.nextAddCash
    TypeCast(self.node:getChildByTag(1011), "CCLabelTTF"):setString(string.format("使用%d军饷淘金，获得%d现金！", self.coin, self.cash))
  else
    showMessage(res.msg, ccc3(255,255,0))
    self.node:getParent():removeChild(self.node, true)
  end
end
AddPowerCashSceneFunc.onAddCashButton = function(self, node, event)
  --TypeCast(self.node:getChildByTag(1020), "CCButton"):setEnabled(false)
  LoadScene:showLoadingScene()
  NetworkRequest.exchangeCash(self.coin, self.cash, "onAddCashComplete", self)
end
AddPowerCashSceneFunc.onAddCashComplete = function(self, data)
  print("AddPowerCashSceneFunc.onAddCashComplete", data)
  LoadScene:removeLoadingScene()
  --TypeCast(self.node:getChildByTag(1020), "CCButton"):setEnabled(true)
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    self.coin = res.data.nextPayCoin
    self.cash = res.data.nextAddCash
    TypeCast(self.node:getChildByTag(1011), "CCLabelTTF"):setString(string.format("使用%d军饷淘金，获得%d现金！", self.coin, self.cash))
  else
    showMessage(res.msg, ccc3(255,255,0))
  end 
end
